/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023.
 * Description: support multi-threads unpack initramfs
 * Author: yangzhuohao <yangzhuohao1@huawei.com>
 * Create: 2023-03-07
 */
#ifndef MULTI_THREADS_INITRAMFS_H
#define MULTI_THREADS_INITRAMFS_H
#include <linux/types.h>
#include <linux/file.h>
#include <linux/time64.h>
#include <linux/limits.h>

#define  MAX_FILES          10
struct rootfs_info {
	u64   offset;
	u64   len;
};

struct multi_rootfs_header {
	u32  magic_num;
	u32  header_size;
	u32  cpio_num;
	struct rootfs_info info[MAX_FILES];
};

struct dir_entry {
	struct list_head list;
	char *name;
	time64_t mtime;
};

enum state {
	Start,
	Collect,
	GotHeader,
	SkipIt,
	GotName,
	CopyFile,
	GotSymlink,
	Reset
};

#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)

struct hash {
	int ino, minor, major;
	umode_t mode;
	struct hash *next;
	char name[N_ALIGN(PATH_MAX)];
};

bool __init is_multi_rootfs(struct multi_rootfs_header *header);
char * __init multi_threads_unpack_to_rootfs(struct multi_rootfs_header *header);

ssize_t __init xwrite(struct file *file, const char *p, size_t count,
		loff_t *pos);
int hash(int major, int minor, int ino);
#ifdef CONFIG_IMA_DIGEST_LIST
static int __init do_setxattrs(char *pathname, char *buf, size_t size);
#endif
void __init clean_path(char *path, umode_t fmode);
long __init do_utime(char *filename, time64_t mtime);
#endif /* MULTI_THREADS_INITRAMFS */
